package vip.xumy.admin.um.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.log4j.Log4j2;
import vip.xumy.admin.um.pojo.Permission;
import vip.xumy.admin.um.pojo.Role;
import vip.xumy.admin.um.service.IPermissionService;
import vip.xumy.admin.um.service.IRolePermissionService;
import vip.xumy.admin.utils.AdminLogUtil;
import vip.xumy.core.exception.CoreException;
import vip.xumy.core.pojo.base.BasePageParam;
import vip.xumy.core.pojo.com.AjaxResponse;
import vip.xumy.core.pojo.com.PageResponse;

/**	Ownership belongs to the company
 *	author:mengyxu
 *	date:2019年12月2日
 */

@Log4j2
@RestController
@RequestMapping("admin")
public class PermissionController {
	@Autowired
	private IPermissionService perService;
	@Autowired
	private IRolePermissionService roleService;
	
	@RequestMapping(value = "per/list", method = RequestMethod.GET)
	public List<Permission> queryPermissionTree(HttpServletRequest request) throws CoreException{
		log.info("查询所有权限");
		return perService.queryPermissionTree(true);
	}
	
	@RequestMapping(value = "per/save", method = RequestMethod.POST)
	public AjaxResponse savePermission(@RequestBody Permission permission) throws CoreException{
		log.info("添加权限");
		perService.save(permission);
		return new AjaxResponse(true, "权限添加成功！");
	}
	
	@RequestMapping(value = "per/delete", method = RequestMethod.POST)
	public AjaxResponse deletePermission(@RequestBody Permission permission){
		log.info("删除权限");
		perService.delete(permission.getId());
		return new AjaxResponse(true, "权限删除成功！");
	}
	
	@RequestMapping(value = "per/update", method = RequestMethod.POST)
	public AjaxResponse updatePermission(@RequestBody Permission permission) throws CoreException{
		log.info("修改权限");
		perService.update(permission);
		return new AjaxResponse(true, "权限修改成功！");
	}
	
	@RequestMapping(value = "role/list")
	public PageResponse<Role> queryRoleList(BasePageParam example){
		log.info("查询角色列表");
		Page<Role> pages = PageHelper.startPage(example.getPage(), example.getSize());
		List<Role> list = roleService.queryList();
		PageResponse<Role> rsp = new PageResponse<>();
		rsp.setRows(list);
		rsp.setTotal(pages.getTotal());
		return rsp;
	}
	
	@RequestMapping(value = "role/list/all")
	public PageResponse<Role> queryAll(BasePageParam example){
		log.info("查询所有角色");
		Page<Role> pages = PageHelper.startPage(example.getPage(), example.getSize());
		List<Role> list = roleService.queryAll();
		PageResponse<Role> rsp = new PageResponse<>();
		rsp.setRows(list);
		rsp.setTotal(pages.getTotal());
		return rsp;
	}
	
	@RequestMapping(value = "role/save", method = RequestMethod.POST)
	public AjaxResponse saveRole(@RequestBody Role role, HttpServletRequest request){
		AdminLogUtil.addRole(request, role);
		roleService.save(role);
		return new AjaxResponse(true, "角色添加成功！");
	}
	
	@RequestMapping(value = "role/delete", method = RequestMethod.POST)
	public AjaxResponse deleteRole(@RequestBody Role role, HttpServletRequest request){
		AdminLogUtil.deleteRole(request, role);
		roleService.delete(role.getCode());
		return new AjaxResponse(true, "角色删除成功！");
	}

	@RequestMapping(value = "role/update", method = RequestMethod.POST)
	public AjaxResponse updateRole(@RequestBody Role role, HttpServletRequest request) throws CoreException{
		AdminLogUtil.updateRole(request, role);
		String msg = roleService.update(role);
		return new AjaxResponse(true, msg);
	}

}
